package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC974 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
         * @param 此题目参考了别人代码
         * 这是因为问题情况较为复杂
         * 未来需要再次复习此道题目
         * @return:
         */
    class Solution {
        public int subarraysDivByK(int[] A, int K) {
            Map<Integer, Integer> record = new HashMap<>();
            record.put(0, 1);
            int sum = 0, ans = 0;
            for (int elem : A) {
                sum += elem;
                // 注意 Java 取模的特殊性，当被除数为负数时取模结果为负数，需要纠正
                int modulus = (sum % K + K) % K;
                int same = record.getOrDefault(modulus, 0);
                ans += same;
                record.put(modulus, same + 1);
            }
            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[4,5,0,-2,-3,1]");
        System.out.println(solution.subarraysDivByK(arr, 5));
    }

    public static void main(String[] args) throws Exception {
        LC974 an = new LC974();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}


